// pages/buy/index.js
import Config from '../../etc/config'
var app = getApp()
const {
  $Toast
} = require('../../components/dist/base/index');
Page({
  data: {
    checkItems: [],
    currentIndex: 0,
    currentCollocation: [],
    btnNextName: '下一步',
    merTypeId: '',
    addressId: '',
    sex: [{
        image: '/images/icon/girl.png',
        value: '女生',
        bgCorlor: 'background:#FF8C91',
        ischecked: true
      },
      {
        image: '/images/icon/boy.png',
        value: '男生',
        bgCorlor: 'background:#8DA1FF',
        ischecked: false
      }
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.merTypeId = options.typeId
    if (wx.getStorageSync('userId')) {
      this.getAdImg(options.type)
    } else {
      wx.navigateTo({
        url: '../authorize/index',
      })
    }
  },

  // 获取默认地址和页面广告图
  getAdImg(type) {
    let params = {
      userId: wx.getStorageSync('userId'),
      typeId: type
    }
    let token = wx.getStorageSync("token");
    Config._post('/merServicesType/getImgStrByTypeId', token, params).then(res => {
      if (res.data.path) {
        this.setData({
          img: res.data.path
        })
      }
    })
  },
  // 获取门店
  getShop(val) {
    let params = {
      merSerTypeId: this.data.merTypeId,
      sex: val
    }
    let token = wx.getStorageSync("token");
    Config._post('/merServicesType/getMerSerTypeDetail', token, params).then(res => {
      if (res.code === 0) {
        this.setData({
          collocationList: res.data.collocationList,
          currentIndex: 0
        })
        // if (this.data.collocationList != null && this.data.collocationList.length > 0) {
        //   wx.setNavigationBarTitle({
        //     title: this.data.collocationList[0].name
        //   })
        // }
      }
    })
  },

  // 选择性别
  selectSex(e) {
    if (!this.data.sex[e.currentTarget.dataset.index].ischecked){
      for (let i = 0; i < 2; i++) {
        this.data.sex[i].ischecked = !this.data.sex[i].ischecked
      }
      this.setData({
        sex: this.data.sex
      })
    }
  },

  // 选择
  handlerCollItem(e) {
    let proper = this.data.collocationList
    let idx = e.currentTarget.dataset.index
    if (proper && proper.length > 0 && this.data.currentIndex < proper.length) {
      let collItemProper = proper[this.data.currentIndex].properties
      if (proper[this.data.currentIndex].idMulti == 1) {
        if (collItemProper[idx].ischecked == undefined) {
          collItemProper[idx].ischecked = false
        }
        // 多选
        collItemProper[idx].ischecked = !collItemProper[idx].ischecked
      } else {
        // 单选 
        for (let i = 0; i < collItemProper.length; i++) {
          if (collItemProper[i].ischecked == undefined) {
            collItemProper[i].ischecked = false
          }
          if (i == idx) {
            collItemProper[i].ischecked = true
          } else {
            collItemProper[i].ischecked = false
          }
        }
      }
      this.setData({
        collocationList: proper
      })
    }
  },
  //判断当前类型是否被选中
  collItemIsChecked() {
    let data = this.data.collocationList[this.data.currentIndex].properties
    for (let i = 0; i < data.length; i++) {
      if (data[i].ischecked) {
        return true;
      }
    }
  },

  // 进入身形定制
  goCustomization() {
    for (let i = 0; i < 2; i++) {
      if (this.data.sex[i].ischecked) {
        this.getShop(i)
      }
    }
  },

  // 下一步
  handlerNext() {
    if (!this.collItemIsChecked()) {
     $Toast({
        content: '未选中'
      })
      return;
    }
    if (this.data.collocationList) {
      if (this.data.currentIndex >= this.data.collocationList.length - 1) {
        for (let i = 0; i < this.data.collocationList.length; i++) {
          let options = {
            name: this.data.collocationList[i].name,
            value: []

          }
          for (let j = 0; j < this.data.collocationList[i].properties.length; j++) {
            if (this.data.collocationList[i].properties[j].ischecked) {
              options.value.push(this.data.collocationList[i].properties[j].label)
            }
          }
          this.data.checkItems.push(options)
        }
        getApp().globalData.collList = this.data.checkItems
        //跳转到身型定制页面
        wx.navigateTo({
          url: '../bodycustomization/index?merTypeId=' + this.data.merTypeId
        })
        return
      }
      let offIndex = this.data.currentIndex + 1
      this.setData({
        currentIndex: offIndex
      })
      // wx.setNavigationBarTitle({
      //   title: this.data.collocationList[offIndex].name
      // })
    } else {
      for (let i = 0; i < 2; i++) {
        if (this.data.sex[i].ischecked) {
          this.getShop(i)
        }
      }
    }
  },
  // 上一步
  handlerPre() {
    if (this.data.currentIndex <= 0) {
      return
    }
    let offIndex = this.data.currentIndex - 1
    this.setData({
      currentIndex: offIndex
    })
    // wx.setNavigationBarTitle({
    //   title: this.data.collocationList[offIndex].name
    // })
  },
  onShareAppMessage: function() {
    return {
      // title: wx.getStorageSync('mallName') + '——' + app.globalData.shareProfile,
      title: app.globalData.shareTitle,
      path: '/pages/collocation/index?type=' + this.data.type + '&typeId=' + this.data.typeId,
      imageUrl: this.data.img,
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})